/**
 * Created by public on 2017/11/30.
 * @param needRequest:是否有必要去请求 true:需要 false：不需要
 * @param dataSource：如果needRequest为false,提供数据来源
 * @param params：公告中每条的id，从dataSource中找到对应id对应的图片数据
 * @param requestUrl：请求的地址
 * @param needRemark:是否需要在关闭按钮触发时标记 true:需要 false：不需要
 * @param needRemarkIds:关闭按钮触发时标记该条公告的id
 * @param containers：由于项目中有的存在iframe中，所以要在这个容器下找目标对象
 */
(function (root, factory, pluginName) {
    factory(root.jQuery, pluginName);
})(window, function ($, pluginName) {
    var _PROTOTYPE_ = {
        _init: function () {
            if (!this.needRequest && this.dataSource.length) {
                if (this.params) {
                    for (var i = 0; i < this.dataSource.length; i++) {
                        if (this.params == this.dataSource[i].id) {
                            this.result = this.dataSource[i].pictureList;
                            break;
                        }
                    }
                    if (this.result.length) {
                        this._generate();
                        this._render();
                    }
                } else {
                    this.result = this.dataSource;
                    if (this.result.length) {
                        this._generate();
                        this._render();
                    }
                }
            } else if (this.needRequest && this.requestUrl) {
                this._load();
            }

            /*this._load();*/
        },
        _generate: function () {
            var html = "<div class=\"step-container\">" +
                       "<div class=\"close-photoGallery\">" +
                       "<img src='/views/yxtPc/assets/img/close.png'>" +
                       "</div>" +
                       "<div class=\"left-cover arrow\">" +
                       "<div class=\"previous-arrow\">" +
                       "<img src='/views/yxtPc/assets/img/L0.png'>" +
                       "</div>" +
                       "</div>" +
                       "<div class=\"step-play-container\">" +
                       "<ul class=\"clearfix\">" +
                       /*"<li class=\"photo-list active\"><a href=\"\"><img src=\"../assets/img/01.png\"></a></li>"+
                        "<li class=\"photo-list\"><a href=\"\"><img src=\"../assets/img/02.png\"></a></li>"+
                        "<li class=\"photo-list\"><a href=\"\"><img src=\"../assets/img/03.png\"></a><</li>"+*/
                       "</ul>" +
                       "</div>" +
                       "<div class=\"right-cover arrow\">" +
                       "<div class=\"next-arrow\">" +
                       "<img src='/views/yxtPc/assets/img/R0.png'>" +
                       "</div>" +
                       "</div>" +
                       "<div class=\"step-pager\">" +
                       "<ul class=\"clearfix\">" +
                       /*"<li class=\"step-pager-list\">1</li>"+
                        "<li class=\"step-pager-list\">2</li>"+
                        "<li class=\"step-pager-list\">3</li>"+
                        "<li class=\"step-pager-list\">4</li>"+
                        "<li class=\"step-pager-list\">5</li>"+*/
                       "</ul>" +
                       "</div>" +
                       "</div>";
            $(this).append(html).show();
            this.$stepPlayContainer =
            this.containers ? this.containers.find(".step-play-container")
                : $(".step-play-container");
            this.$stepPager =
            this.containers ? this.containers.find(".step-pager") : $(".step-pager");
            this.$arrow = this.containers ? this.containers.find(".arrow") : $(".arrow");
            this.$leftArrow =
            this.containers ? this.containers.find(".left-cover") : $(".left-cover");
            this.$rightArrow =
            this.containers ? this.containers.find(".right-cover") : $(".right-cover");
            this.$closePhotoGallery =
            this.containers ? this.containers.find(".close-photoGallery")
                : $(".close-photoGallery");
            this._closeBind();
        },
        _render: function () {
            this.total = this.result.length;
            for (var i = 0; i < this.total; i++) {
                var liHtml = "<li class='photo-list' photo-index='" + i
                             + "'><img src='/views/yxtPc/assets/img/loading.gif' class='img-responsive loading-default'><img src='"
                             + this.result[i] + "' class='img-responsive loading-success'></li>";
                var liPager = "<li class='step-pager-list' pager-index='" + i + "'>" + (i + 1)
                              + "</li>";
                this.$stepPlayContainer.find("ul").append(liHtml);
                this.$stepPager.find("ul").append(liPager);
            }
            this._imgLoaded();
            this.$stepPlayContainer.find("ul li").eq(0).addClass("active");
            this.$stepPager.find("ul li").eq(0).addClass("active-pager");
            this.curr = 0;
            this._adjustScreen();
            this._showArrow(this.total, this.curr);
            this._arrowBind();
            this._bindScroll();
            this._pagerBind();
        },
        _load: function () {
            var _this = this;
            $.ajax({
                       url: _this.requestUrl,
                       type: 'get',
                       dataType: 'json',
                       success: function (data) {
                           _this.result = data.data;
                           if (_this.result.length) {
                               _this._generate();
                               _this._render();
                           }
                       },
                       error: function (XMLHttpRequest, textStatus, errorThrown) {
                           console.log(textStatus);
                       }
                   });
        },
        _imgLoaded: function () {
            this.$loadingSuccess =
            this.containers ? this.containers.find(".loading-success") : $(".loading-success");
            this.$loadingSuccess.each(function () {
                $(this).load(function () {
                    var $_this = $(this);
                    $_this.show().prev().hide();
                });
            });
        },
        _showArrow: function (total, curr) {
            if (total == 1 && curr == 0) {
                this.$arrow.hide();
            } else if (total > 1 && curr == total - 1) {
                this.$leftArrow.show();
                this.$rightArrow.hide();
            } else if (total > 1 && curr == 0) {
                this.$leftArrow.hide();
                this.$rightArrow.show();
            } else if (total > 1) {
                this.$leftArrow.show();
                this.$rightArrow.show();
            }
        },
        _arrowBind: function () {
            var _this = this;
            this.$arrow.on("click", function () {
                if ($(this).hasClass("left-cover")) {
                    _this.curr--;
                    _this._showArrow(_this.total, _this.curr);
                    _this.$stepPlayContainer.find("ul li").eq(_this.curr).addClass("active").siblings().removeClass("active");
                    _this.$stepPager.find("ul li").eq(_this.curr).addClass("active-pager").siblings().removeClass("active-pager");
                } else if ($(this).hasClass("right-cover")) {
                    _this.curr++;
                    _this._showArrow(_this.total, _this.curr);
                    _this.$stepPlayContainer.find("ul li").eq(_this.curr).addClass("active").siblings().removeClass("active");
                    _this.$stepPager.find("ul li").eq(_this.curr).addClass("active-pager").siblings().removeClass("active-pager");

                }
            });
        },
        _bindScroll: function () {
            var _this = this;
            var scrollBool = true;
            this.$stepPlayContainer.on("mousewheel", function (event) {
                var direction = event.originalEvent.deltaY > 0 ? 1 : -1;
                if (scrollBool) {
                    if (direction == 1 && _this.curr != _this.total - 1) {
                        scrollBool = false;
                        _this.curr++;
                        _this._showArrow(_this.total, _this.curr);
                        _this.$stepPlayContainer.find("ul li").eq(_this.curr).addClass("active").siblings().removeClass("active");
                        _this.$stepPager.find("ul li").eq(_this.curr).addClass("active-pager").siblings().removeClass("active-pager");
                        setTimeout(function () {
                            scrollBool = true;
                        }, 300);
                    } else if (direction == -1 && _this.curr != 0) {
                        scrollBool = false;
                        _this.curr--;
                        _this._showArrow(_this.total, _this.curr);
                        _this.$stepPlayContainer.find("ul li").eq(_this.curr).addClass("active").siblings().removeClass("active");
                        _this.$stepPager.find("ul li").eq(_this.curr).addClass("active-pager").siblings().removeClass("active-pager");
                        setTimeout(function () {
                            scrollBool = true;
                        }, 300);
                    }
                }
                return false;
            });
        },
        _adjustScreen: function () {
            var winW = window.screen.height;
            this.$photoList =
            this.containers ? this.containers.find(".photo-list") : $(".photo-list");
            this.$photoList.find("img").css("height", (winW - 100));
        },
        _pagerBind: function () {
            var _this = this;
            this.$stepPagerList =
            this.containers ? this.containers.find(".step-pager-list") : $(".step-pager-list");
            this.$stepPagerList.on("click", function () {
                _this.curr = $(this).attr("pager-index");
                _this._showArrow(_this.total, _this.curr);
                $(this).addClass("active-pager").siblings().removeClass("active-pager");
                _this.$stepPlayContainer.find("ul li").eq(_this.curr).addClass("active").siblings().removeClass("active");
            });
        },
        _closeBind: function () {
            var _this = this;
            this.$closePhotoGallery.on("click", function () {
                $(_this).children().remove().end().hide();
                if (_this.needRemark) {
                    $.ajax({
                               url: '/notice/haveReceivedNotice',
                               type: 'get',
                               data: {"noticeIds": _this.needRemarkIds},
                               dataType: 'json',
                               success: function (data) {
                                   if (data.result) {
                                       console.log(data.msg);
                                   }
                               },
                               error: function (XMLHttpRequest, textStatus,
                                                errorThrown) {
                                   console.log(textStatus);
                               }

                           });
                }
            });
        }
    }
    $.fn[pluginName] = function (options) {
        $.extend(this, _PROTOTYPE_, options);
        this._init();
    }
}, 'photoScroll');